package com.example.viewmodel

import android.accounts.Account
import androidx.lifecycle.viewModelScope
import com.example.lib_mvicore.viewmodel.BaseViewModel
import com.example.libraryaccount.intent.AccountIntent
import com.example.libraryaccount.model.service.AccountService
import com.example.libraryaccount.state.AccountState

import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
import kotlin.coroutines.coroutineContext

/**
 * @author ytx 
 * @createDate 2023/11/10 16:39
 * @description
 *
 * @updateUser ytx
 * @updateDate 2023/11/10 16:39
 * @updateRemark
 *
 * @version 1.0.0
 */
class AccountViewModel: BaseViewModel<AccountIntent>() {
    val service: AccountService by lazy { AccountService()}

    private val _state= MutableStateFlow<AccountState>(AccountState.AccountInit)
    val state: StateFlow<AccountState>
        get() = _state

    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect {
                when(it){
                    is AccountIntent.FindAccountByIdIntent -> {
                        doFindAccountByIdIntent(it)
                    }
                    is AccountIntent.FindAccountByUserIdIntent -> {
                        doFindAccountByUserIdIntent(it)
                    }
                    is AccountIntent.ModifyCoinAmount -> {
                        doModifyCoinAmount(it)
                    }
                }
            }
        }
    }

    private fun doModifyCoinAmount(it: AccountIntent.ModifyCoinAmount) {
        viewModelScope.launch(Dispatchers.IO+ CoroutineExceptionHandler{coroutineContext,throwable->{
            
        }}) {
            val result = service.modifyCoinAmount(it.id,it.nm)
            if (result.code==0){
                _state.value=AccountState.ModifyCoinAmountSuccessState
            }else{
                _state.value= AccountState.ModifyCoinAmountFailedState
            }
        }
    }

    private fun doFindAccountByUserIdIntent(it: AccountIntent.FindAccountByUserIdIntent) {
        viewModelScope.launch(Dispatchers.IO) {
            val result = service.findAccountByUserId(it.userId)
            if (result.code==0){
                _state.value=AccountState.AccountSuccessState(result.data)
            }else{
                _state.value= AccountState.AccountFailedState
            }
        }
    }

    private fun doFindAccountByIdIntent(it: AccountIntent.FindAccountByIdIntent) {
        viewModelScope.launch(Dispatchers.IO) {
            val result = service.findAccountById(it.id)
            if (result.code==0){
                _state.value=AccountState.AccountSuccessState(result.data)
            }else{
                _state.value= AccountState.AccountFailedState
            }
        }
    }
}